package tw.edu.tku.im.maze.database;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import tw.edu.tku.im.maze.EnterRoomRep;
import tw.edu.tku.im.maze.LeaveRoomRep;
import tw.edu.tku.im.maze.LeaveRoomReq;
import tw.edu.tku.im.maze.Packet;

public class LeaveRoomProcessor extends PacketProcessor {

   @Override
   String acceptName() {
      return "leaveRoom";
   }

   @Override
   Packet process(Packet packet) {
    LeaveRoomReq req = (LeaveRoomReq)packet;
    Connection con;
    try {
       con = DriverManager.getConnection(address,user,passwd);
       con.setCatalog(database);
       CallableStatement cstmt = 
             con.prepareCall("{call usp_leaveRoom(?,?,?)}");
       cstmt.setInt(1, req.getRoomID());
       cstmt.setString(2, req.getUserID());
       cstmt.setInt(3, req.getTicket());
       cstmt.execute();
       return new LeaveRoomRep(0);
    } catch (SQLException e) {
       e.printStackTrace();
    }
    return new LeaveRoomRep(1);
   }

}
